﻿using System.Collections.Generic;
using System.Runtime.Caching;
using System.Web.Mvc;
using Bll.Sys;
using IBll;
using Model.Database;
using Model.Sys;
using Model.Sys.Config;
using Model.Sys.Constant;

namespace Controllers.Extend
{
    /// <summary>
    /// 基础控制器类
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// Spring属性注入CurrentBllSession
        /// </summary>
        public IBllSession CurrentBllSession { get; set; }


        /// <summary>
        /// 执行前进行共性操作
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            bool siteConfigCacheIsExist = CurrentBllSession.CacheBll.IsExists(Constant.CacheKey.SiteConfigCacheKey);
            bool dataDictionaryCacheIsExist = CurrentBllSession.CacheBll.IsExists(Constant.CacheKey.DataDictionaryCacheKey);

            //站点信息
            if (!siteConfigCacheIsExist)
            {
                SiteConfig siteConfig= CurrentBllSession.SiteConfigBll.LoadConfig(Constant.PhysicalPath.SiteConfigPhysicalPath);
                CurrentBllSession.CacheBll.Add(Constant.CacheKey.SiteConfigCacheKey, siteConfig);
                if ( siteConfig.SiteStatus == false) //关闭站点
                {
                    filterContext.Result = new RedirectResult("/Error/SiteClose");
                }
            }

            //数据字典
            if (!dataDictionaryCacheIsExist)
            {
                List<DataDictionaryHead> dataDictionaryHeadList = CurrentBllSession.DataDictionaryHeadBll.GetDataDictionary();
                CurrentBllSession.CacheBll.Add(Constant.CacheKey.DataDictionaryCacheKey, dataDictionaryHeadList);
            }
        }

        /// <summary>
        /// 返回结果前附加数据
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            
        }

    }
}
